//
//  LoginViewController.m
//  CityFire
//
//  Created by liuyangzhu on 2017/8/2.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "LoginViewController.h"
#import "UserInfoViewModel.h"
#import <FCUUID/FCUUID.h>
#import "NSString+Tools.h"
#import "IPToolManager.h"

@interface LoginViewController ()

@property (nonatomic, weak) IBOutlet UITextField *userNameTF;
@property (nonatomic, weak) IBOutlet UITextField *psdTF;
@property (nonatomic, weak) IBOutlet UIButton    *loginBut;

@property (nonatomic, strong) UserInfoViewModel *userInfoViewModel;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#DDDDDD"];
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:@"请输入用户名" attributes:dict];
    [self.userNameTF setAttributedPlaceholder:attribute];
    
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
    dict2[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#DDDDDD"];
    NSAttributedString *attribute2 = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:dict2];
    [self.psdTF setAttributedPlaceholder:attribute2];
    
    [self bindViewMode];
}


/**
 绑定数据监控变化
 */
- (void)bindViewMode {
    [RACObserve(self.userInfoViewModel, userInfoModel) subscribeNext:^(UserInfoModel *userInfoModel) {
        DDLogInfo(@"login___userInfomodel%@", userInfoModel);
    }];
    
    [RACObserve(self.userInfoViewModel, responseInfo) subscribeNext:^(NSError *responseInfo) {
        if (responseInfo == nil) {
            return ;
        }
        
        if (responseInfo.code == 0) {
            [self dismissViewControllerAnimated:YES completion:^{
            }];
        }
        DDLogInfo(@"login___responseInfo%@", responseInfo);
    }];
}

/**
 登录

 @param sender sender
 */
- (IBAction)loginClick:(id)sender
{
    
    [self.view endEditing:YES];
    if (!self.userNameTF.text.length) {
        [SVProgressHUD showInfoWithStatus:kWARN_USERNAME_INFO];
        [self.userNameTF becomeFirstResponder];
        
        return;
    }
    
    if (!self.psdTF.text.length) {
        [SVProgressHUD showInfoWithStatus:kWARN_PASSWORD_INFO];
        [self.psdTF becomeFirstResponder];
        
        return;
    }
    
    [self.userInfoViewModel userLogin:@{@"username" : self.userNameTF.text, @"password" : [NSString md5:self.psdTF.text], @"os" : @"ios", @"clientId" : [FCUUID uuidForDevice], @"ip" : [[IPToolManager sharedManager] currentIpAddress],@"sessionid" : @"1"}];
}

- (UserInfoViewModel *)userInfoViewModel
{
    if (_userInfoViewModel == nil) {
        _userInfoViewModel = [[UserInfoViewModel alloc] init];
    }
    
    return _userInfoViewModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
